<template>
  <div class="container">
    <div class="app-container">
      <el-tree
        :data="depts"
        default-expand-all
        :expand-on-click-node="false"
        :props="defaultProps"
      >
        <template #default="{data}">
          <el-row
            style="width: 100%; height: 40px"
            type="flex"
            justify="space-between"
            align="middle"
          >
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="manager">{{ data.managerName }}</span>
              <!-- 下拉菜单 -->
              <!-- $even 固定的实参 -->
              <el-dropdown @command="operateDept($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>

        </template>
      </el-tree>
    </div>
    <!-- 新增部门 -->
    <!--
      .sync
      接收子组件派发过来的事件和值
      :show-dialog.sync ==== :show-dialog="showDialog" + @update:showDialog="showDialog = false"
     -->
    <add-dept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :current-id="currentId"
      @updateDepartment="getDepartment"
    />
  </div>
</template>
<script>
import { transListToTreeData } from '@/utils/index'
import { getDepartment, delDepartment } from '@/api/department'
import AddDept from './AddDept.vue'
export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      depts: [],
      defaultProps: {
        label: 'name'
      },
      showDialog: false, // 控制新增部门的弹层显示or隐藏
      currentId: null
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      const result = await getDepartment()
      console.log(result)
      // 配合该方法 -> 将列表转成树形
      this.depts = transListToTreeData(result, 0)
    },
    operateDept(command, id) {
      // 添加子部门
      if (command === 'add') {
        // 添加部门的弹层显示
        // 修改某个变量 -> 让新增部门的弹层显示
        this.showDialog = true
        this.currentId = id
      } else if (command === 'edit') {
        this.currentId = id
        this.showDialog = true
        // 父组件中调用子组件提供的方法
        // 1. 获取子组件实例
        // 2. 子组件实例身上的方法的调用
        // 调用子组件身上的方法 - 同步
        // 更新props的过程是 - 异步
        // 目的. 确保props更新完毕, 在调用子组件的方法
        this.$nextTick(() => {
          this.$refs.addDept.getDepartmentDetail()
        })
      } else {
        this.$confirm('你确定要删除该部门么?').then(async() => {
          // 发送请求
          await delDepartment(id)
          // 重新获取数据
          this.getDepartment()
        })
      }
    }
  }
}
</script>
<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.manager {
  width: 80px;
  display: inline-block;
  margin-right: 8px;
}
</style>
